package com.maple.phoneward.utils;

import android.content.Context;

import com.maple.phoneward.activity.R;
import com.maple.phoneward.constants.Constants;
import com.maple.phoneward.custom.EmailSender;

/**
 * EmailUtil.java
 * 
 * @author YaJian Qiu
 * @time 2013-4-16 下午8:33:54<br>
 *       本类描述：邮件发送工具类
 * 
 */
public class EmailUtil {

	private static final String TAG = "EmailUtil";

	/**
	 * @param context
	 * @param title
	 *            邮件标题
	 * @param content
	 *            邮件内容
	 * @param attachPaths
	 *            附件路径
	 */
	public static final void sendEmail(final Context context, final String title, final String content, final String... attachPaths) {

		if (!NetUtil.isNetWorkAvailable(context)) {
			Logger.d(TAG, "邮件发送失败.....");
			return;
		}
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Logger.d(TAG, "开始邮件发送.....");
					EmailSender sender = new EmailSender(context);
					// 设置服务器地址和端口
					sender.setProperties(context.getString(R.string.email_smtp_address), context.getString(R.string.email_smtp_port));
					// 分别设置发件人，邮件标题和文本内容
					sender.setMessage(context.getString(R.string.email_from), context.getString(R.string.app_name) + "----" + title, content);
					Logger.d(TAG, "邮件内容[" + content + "]");
					// 设置收件人
					sender.setReceiver(AppUtil.getFromSharedPreferences(context, Constants.CONFIG_EMAIL));
					// 添加附件
					if (attachPaths != null && attachPaths.length != 0) {
						for (String path : attachPaths) {
							// 判断文件是否存在
							if (FileUtil.isFileExists(path))
								sender.addAttachment(path);
						}
					}
					// 发送邮件
					sender.sendEmail(context.getString(R.string.email_smtp_address), context.getString(R.string.email_from), "365admin");// 密码为了安全写死在代码里
					Logger.d(TAG, "邮件发送完成.....");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
}
